// pages/Census/customer-list.js
const Controllers = require('../../controllers/ControllerCensus').controller;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    details: [
      {
        value: 10000,
        text: '客户数量',
        key: 'shopNum'
      },
      {
        value: 10000.00,
        text: '租金收益',
        unit: '¥',
        key: 'rent'
      },
      {
        value: 10000.00,
        text: '云商品收益',
        unit: '¥',
        key: 'cloud'
      }
    ],
    page: {
      pageNumber: 1,
      pageSize: 10,
      genre: 0
    },
    list: [],
    totalRow: 0,
    isLoading: false,
    lastPage: true,
    pageData: {}
  },
  agentIncomeDetails() {
    var that = this;
    var page = that.data.page;
    if (that.data.isLoading) {
      return;
    }
    that.data.isLoading = true;
    wx.showLoading();
    var params = { ...page };
    Controllers.agentIncomeDetails(params).then(res => {
      console.log('agentIncomeDetails--res==', res);
      const { code, data, desc } = res;
      const { list, totalRow, lastPage } = data;
      if (page.pageNumber === 1) {
        that.data.list = list;
      } else {
        that.data.list = that.data.list.concat(list);
      }
      that.setData({
        list: that.data.list,
        totalRow,
        lastPage,
        isLoading: false
      });
      wx.hideLoading();
    })
  },
  agentDetail() {
    var that = this;
    var agentId = that.data.agentId;
    Controllers.agentDetail({agentId}).then(res => {
      console.log('agentDetail--res==', res);
      const { code, data, desc } = res;
      that.setData({
        detailData: data
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      agentId: options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.agentDetail();
    that.agentIncomeDetails();
    var agentIndexData = wx.getStorageSync('agentIndex');
    console.log('onShow--agentIndexData==', agentIndexData);
    that.setData({
      agentIndexData
    })
  },
  openPage(e) {
    var url = e.currentTarget.dataset.url;
    if (url) {
      app.openPage({ url });
    } else {
      wx.showToast({
        title: '未获取页面路径',
        icon: 'none'
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.isLoading || that.data.lastPage) {
      return;
    }
    that.data.page.pageNumber += 1;
    that.agentIncomeDetails();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})